﻿//-----------------------------------------------------------------------------
// <copyright file="ShellForm.cs" company="DCOM Productions">
//     Copyright (c) DCOM Productions.  All rights reserved.
// </copyright>
//-----------------------------------------------------------------------------

namespace Setup.Forms {
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using BitFlex.Win32.Structures;
    using BitFlex.Win32;
    using System.IO;

    public partial class ShellForm : Form {
        /// <summary>
        /// Default constructor
        /// </summary>
        public ShellForm() {
            InitializeComponent();
        }

        private void OnAgreementCheckboxCheckChanged(object sender, EventArgs e) {
            uxWizard.SelectedPage.IsValid = uxAgreement.Checked;
        }

        private void OnInstallPathPageNavigatingFrom(object sender, EventArgs e) {
            string path = string.IsNullOrEmpty(uxInstallPath.CueText) ? uxInstallPath.Text : uxInstallPath.CueText;
            if (!Directory.Exists(path)) {
                try {
                    Directory.CreateDirectory(path);
                }
                catch {
                    // invalid path
                }
            }
        }

        private void uxActionPage_NavigatedTo(object sender, EventArgs e) {
            uxWizard.SelectedPage.IsValid = false;
            uxWizard.SetBackState(false);
            uxWizard.SetCancelButtonState(false);
            uxActionText.Text = "Preparing...";
            Dictionary<string, byte[]> filesToCopy = new Dictionary<string, byte[]>();
            filesToCopy.Add("BitFlex.dll", Properties.Resources.BitFlex);
            filesToCopy.Add("BitFlex.CompositeUI.dll", Properties.Resources.BitFlex_CompositeUI);
            filesToCopy.Add("BitFlex.CompositeUI.WinForms.dll", Properties.Resources.BitFlex_CompositeUI_WinForms);
            filesToCopy.Add("BitFlex.EnterpriseServices.dll", Properties.Resources.BitFlex_EnterpriseServices);
            filesToCopy.Add("BitFlex.Instrumentation.dll", Properties.Resources.BitFlex_Instrumentation);
            filesToCopy.Add("BitFlex.Internal.dll", Properties.Resources.BitFlex_Internal);
            filesToCopy.Add("BitFlex.IO.dll", Properties.Resources.BitFlex_IO);
            filesToCopy.Add("BitFlex.ObjectBuilder.dll", Properties.Resources.BitFlex_ObjectBuilder);
            filesToCopy.Add("BitFlex.User32.dll", Properties.Resources.BitFlex_User32);
            filesToCopy.Add("BitFlex.Win32.dll", Properties.Resources.BitFlex_Win32);
            filesToCopy.Add("BitFlex.Windows.Forms.dll", Properties.Resources.BitFlex_Windows_Forms);
            filesToCopy.Add("BitFlex.Xdf.dll", Properties.Resources.BitFlex_Xdf);
            uxProgressBar.Maximum = 0;
            foreach (byte[] file in filesToCopy.Values) {
                uxProgressBar.Maximum += file.Length;
            }
            foreach (KeyValuePair<string, byte[]> file in filesToCopy) {
                string path = string.Empty;
                if (string.IsNullOrEmpty(uxInstallPath.Text)) { // Use CueText
                    path = Path.Combine(uxInstallPath.CueText, file.Key);
                }
                else { // Use Text
                    path = Path.Combine(uxInstallPath.Text, file.Key);
                }
                using (FileStream fstream = new FileStream(path, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite)) {
                    for (int i = 0; i < file.Value.Length; i++) {
                        fstream.WriteByte(file.Value[i]);
                        uxActionText.Text = string.Format("Installing {0}", file.Key);
                        uxProgressBar.Value++;
                        Application.DoEvents();
                    }
                }
            }
            uxActionText.Visible = false;
            uxProgressBar.Visible = false;
            uxWizard.SelectedPage.IsValid = true;
            uxActionPage.Title = "Success";
            uxActionPage.Subtitle = "BitFlex Framework 3.0 has been successfully installed on your system.";
        }

        private void OnWizardFinished(object sender, EventArgs e) {
            Close();
        }
    }
}
